# Maintainer: Junjie Mao <eternal.n08@gmail.com>

_realname=coq
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=8.5
pkgrel=1
pkgdesc="A formal proof management system (mingw-w64)"
arch=('any')
url='https://coq.inria.fr/'
license=('LGPL 2.1')
depends=("${MINGW_PACKAGE_PREFIX}-ocaml"
         "${MINGW_PACKAGE_PREFIX}-ocaml-camlp4")
source=("https://coq.inria.fr/distrib/V${pkgver}/files/${_realname}-${pkgver}.tar.gz"
        "0001-Guess-the-libraries-are-located-at-lib-coq-instead-o.patch")
sha256sums=('89a92fb8b91e7cb0797d41c87cd13e4b63bee76c32a6dcc3d7c8055ca6a9ae3d'
            '5563ac6c5fd3b3124b68189e908c762eea12ca04c96cb770b1414889d26b42d9')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  #patch -p1 -i ${srcdir}/0001-Guess-the-libraries-are-located-at-lib-coq-instead-o.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  ./configure \
      -prefix ${MINGW_PREFIX} \
      -bindir ${MINGW_PREFIX}/bin \
      -libdir ${MINGW_PREFIX}/lib/coq \
      -mandir ${MINGW_PREFIX}/share/man \
      -configdir ${MINGW_PREFIX}/etc/xdg/coq \
      -datadir ${MINGW_PREFIX}/share/coq \
      -docdir ${MINGW_PREFIX}/share/doc/coq \
      -emacslib ${MINGW_PREFIX}/share/emacs/site-lisp/coq \
      -coqdocdir ${MINGW_PREFIX}/share/texmf/tex \
      -usecamlp4 -arch win32 -coqide no

  ocamlc -rectypes -w -3 -c -I "+camlp4" -pp 'camlp4orf -impl' -impl tools/compat5.mlp
  ocamlc -rectypes -w -3 -c -I "+camlp4" -pp 'camlp4orf -impl' -impl tools/compat5b.mlp

  make -j4
}

check() {
  # Some tests fail due to the following reasons:
  #   * Some signals are not available
  #   * lockf (lock a file region) fails
  # These cases also fail with the official binaries.
  plain "skip"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  export OCAMLLIB=${MINGW_PREFIX}/lib/ocaml
  make install COQINSTALLPREFIX=${pkgdir}/ OLDROOT=$(cygpath -m /)
  [ -f LICENSE ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  [ -f COPYRIGHT ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  [ -f CREDITS ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
